// pages/user/common.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    range: ['身份证', '护照', '军人证', '港澳台通行证', '台胞证', '回乡证', '户口簿', '出生证明', '台湾通行证'],
    index: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var _this = this, high = 0
    // wx.showLoading({
    //   title: '加载中',
    // })

    // req.request(
    //   api.getUserAllPassenger,
    //   { method: 'POST', data: { user_token: app.globalData.userInfo.user_token } },
    //   function (res) {
    //     wx.hideLoading();
    //     var custom = res.data
    //     for (var i = 0; i < custom.length; i++) {
    //       high = high + 106;
    //     }
    //     _this.setData({ custom: custom, high: high })
    //   }
    // )

   
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  selectPapers(e) {
    var index = e.detail.value;   
    this.setData({ index: index });
  },
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  Passenger(e){
    var id = e.currentTarget.id
    wx.navigateTo({
      url: '/pages/user/addpassenger?id='+id,
    })
  },
  deletePassenger(e){
    var id = e.currentTarget.id, _this = this,index,custom
    console.info(id)
    wx.showLoading({
      title: '删除中',
    })
    req.request(
      api.deleteUserPassenger,
      { method: 'POST', data: { person_id: id } },
      function (res) {
        wx.hideLoading();
        res = res.data
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
        if(res.id != 0) {
          custom = _this.data.custom
          for(var i in custom){
            if(custom[i].person_id == id) index = i
          }
          custom.splice(index,1)
          _this.setData({custom: custom})
        }
        
      }
    )

  },
  Address(e) {
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/user/address?id=' + id,
    })
  },
  Voucher() {
    wx.navigateTo({
      url: '/pages/user/addvoucher',
    })
  }
})
